package _543cloud;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;


public class _543cloudcount {

	private static class node {
		
		public String str;
		public int score;
		
		public node(String s, int x) {
			
			str = s;
			score = x;
		}
	}
	
	private static class NodeComparator implements Comparator<node>
	{
	    @Override
	    public int compare(node x, node y)
	    {
	    	if (x.score < y.score)
				return -1;
			if (x.score > y.score)
				return 1;
			return 0;
	    }
	}
	
	private class pair {
		
		public String left;
		public String right;
		
		public pair(String left, String right) {
			this.left = left;
			this.right = right;
		}
	}
	
	public static String[] getleftnodes(String filereader) throws IOException{
		FileReader reader;
		ArrayList<String> templist = new ArrayList<String>();
		//try 
		{
			reader = new FileReader(filereader);
			BufferedReader br = new BufferedReader(reader);
			  String row;
			   while((row = br.readLine())!=null){
			    templist.add(row);
			   }
			   String[] inputarray = new String[templist.size()];
			   for (int i = 0; i < templist.size(); i++) {
				   inputarray[i] = templist.get(i);
			}
			   reader.close();
			   br.close();
			   String[] finalrightarrayStrings =  new String[templist.size()];
			   for(int i =0 ;i<templist.size();i++)
			   {
				   String tempString = "";
				   int j = 0;
				   while(templist.get(i).charAt(j) != ','){
					   tempString = tempString+templist.get(i).charAt(j);
					   j++;
				   }
				   finalrightarrayStrings[i] = tempString;
			   }
			   return finalrightarrayStrings;  
			   
		} 
		/*
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		//return null;
	}
	
	public static String[] getrightnodes(String filereader) throws IOException{
		FileReader reader;
		ArrayList<String> templist = new ArrayList<String>();
		//try 
		{
			reader = new FileReader(filereader);
			BufferedReader br = new BufferedReader(reader);
			  String row;
			   while((row = br.readLine())!=null){
			    templist.add(row);
			   }
			   String[] inputarray = new String[templist.size()];
			   for (int i = 0; i < templist.size(); i++) {
				   inputarray[i] = templist.get(i);
			}
			   reader.close();
			   br.close();
			   String[] finalrightarrayStrings =  new String[templist.size()];
			   for(int i =0 ;i<templist.size();i++)
			   {
				   String tempString = "";
				   int j = templist.get(i).length()-1;
				   while(templist.get(i).charAt(j) != ','){
					   tempString = tempString+templist.get(i).charAt(j);
					   j--;
				   }
				   
				   finalrightarrayStrings[i] = tempString;
			   }
			   return finalrightarrayStrings;  
			   
		} 
		/*
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;  
		*/
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		// TODO Auto-generated method stub
		Map<String, HashSet<String> > left_to_right = new HashMap<String, HashSet<String> >();
		Map<String, HashSet<String> > right_to_left = new HashMap<String, HashSet<String> >();
		Map<String, Integer> leftScores = new HashMap<String, Integer>();
		Map<String, Integer> rightScores = new HashMap<String, Integer>();
		
		Map<String, Integer> leftMap = new HashMap<String, Integer>();
		
		Comparator<node> comparator = new NodeComparator();
		PriorityQueue<node> leftQueue = new PriorityQueue<node>(100000, comparator);
		
		String[] leftnodes = null;
		String[] rightnodes = null;
		
		try{
			leftnodes = getleftnodes("src/_543cloud/2.txt");
			rightnodes = getrightnodes("src/_543cloud/2.txt");
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}
			
		for (int i = 0; i < leftnodes.length; i++) {
			
			if (!leftScores.containsKey(leftnodes[i]))
				leftScores.put(leftnodes[i], (int)(Math.random()*30) + 0);
			
			if (!rightScores.containsKey(rightnodes[i]))
				rightScores.put(rightnodes[i], (int)(Math.random()*50) + 50);
			
			if (leftScores.get(leftnodes[i]) <= rightScores.get(rightnodes[i])) {

				if (!left_to_right.containsKey(leftnodes[i])) {
					HashSet<String> hSet = new HashSet<String>();
					hSet.add(rightnodes[i]);
					left_to_right.put(leftnodes[i], hSet);
				}
				else {
					left_to_right.get(leftnodes[i]).add(rightnodes[i]);
				}
				
				if (!right_to_left.containsKey(rightnodes[i])) {
					HashSet<String> hSet = new HashSet<String>();
					hSet.add(leftnodes[i]);
					right_to_left.put(rightnodes[i], hSet);
				}
				else {
					right_to_left.get(rightnodes[i]).add(leftnodes[i]);
				}
			
				if (!leftMap.containsKey(leftnodes[i]))
					leftMap.put(leftnodes[i], 1);
				else
					leftMap.put(leftnodes[i], leftMap.get(leftnodes[i]) + 1);
			}
		}
		
		for (String str : leftMap.keySet()) {
			
			node n = new node(str, leftMap.get(str));
			leftQueue.add(n);
		}
		
		while (!leftQueue.isEmpty()) {
			
			node n = leftQueue.remove();
			String minStr = null;
			int minScore = 101;
			for (Iterator<String> itr = left_to_right.get(n.str).iterator(); itr.hasNext();) {
				String rStr = itr.next();
				if (rightScores.get(rStr) < minScore) {
					minScore = rightScores.get(rStr);
					minStr = rStr;
				}
			}
			
			if (minStr != null) {
				for (Iterator<String> itr = right_to_left.get(minStr).iterator(); itr.hasNext();) {
					String lStr = itr.next();
					if (lStr != n.str) {
						left_to_right.get(lStr).remove(minStr);
					}
				}
				
				System.out.println("pair: " + n.str + " -> " + minStr);
			}
		}
	}

}
